<?php
/**
 * Index.php
 * User: yzm
 * Date: 2017/9/25
 */
class Index extends WebBase{
    public function __construct() {
        parent::__construct();
    }

    /*
     * 首页
     */
    public function index() {
        global $_W,$_GPC;
        $where_sql = " WHERE `uniacid` = :uniacid AND `parent_id` = :parent_id AND `openid` = :openid ";
        $where[':uniacid'] = $_W['uniacid'];
        $where[':parent_id'] = 0;
        $where[':openid'] = $_W['fans']['openid'];

        //是否还有未读消息
        $is_read = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_work_order') . $where_sql . " AND `is_read` = 0 ", $where);

        if ($_W['isajax']) {

            //首页工单总数
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_work_order') . $where_sql, $where);

            //分页数据
            $limit = $_GPC['pageSize'];
            $page = !empty($_GPC['page']) && isset($_GPC['page']) && intval($_GPC['page']) > 1 ? intval($_GPC['page']) : 1;
            $offset = ($page - 1) * $limit;

            $order_list = pdo_fetchall("SELECT * FROM " . tablename('wherein_knowledge_work_order') . $where_sql . " ORDER BY `add_time` DESC LIMIT " . $offset . "," . $limit, $where);
            foreach ($order_list as $key => $value) {
                //replyDetail工单已回复详情; waitingDetail 工单待回复详情 初次提交工单详情页面
                //$parent_id 后台有回复过的
//                $parent_id = pdo_fetchcolumn("SELECT order_id FROM " . tablename('wherein_knowledge_work_order') . " WHERE belong_to = :belong_to AND is_reply = :is_reply ORDER BY order_id DESC ", array(':belong_to' => $value['order_id'], ':is_reply' => 1));
                $act_tmp = ($value['customer_status'] == 1) ? 'waitingDetail' : 'replyDetail';
                $order_list[$key]['url'] = $this->createMobileUrl('order', array('act' => $act_tmp, 'order_id' => $value['order_id'], 'read' => 1));

                $order_list[$key]['add_time'] = format($value['add_time']);
                $order_list[$key]['customer_status_name'] = $this->customer_status[$value['customer_status']];
            }
            $out['order_list'] = $order_list;
            $out['total'] = intval($total);
            $this->outjson($out);
        }

        include $this->template('index');
    }

    /*
     * 常见问题列表
     */
    public function question() {
        global $_W,$_GPC;

        if ($_W['isajax']) {
            //查询当下有没有子分类
            $child_list = pdo_getall('wherein_knowledge_category', array('uniacid' => $_W['uniacid'], 'parent_id' => $_GPC['cat_id']), array('cat_id'));
            $child_list = empty($child_list) ? array() : return_array_value($child_list, 'cat_id');
            array_push($child_list, $_GPC['cat_id']);
            $child_list_implode = implode(',', $child_list);

            //查询问题id
            $where_sql = " WHERE `uniacid` = :uniacid AND `is_common` = 1 AND `cat_id` IN (" . $child_list_implode . ")";
            $where[':uniacid'] = $_W['uniacid'];
            //总数
            $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('wherein_knowledge_question') . $where_sql, $where);

            //分页数据
            $limit = intval($_GPC['pageSize']);
            $page = !empty($_GPC['page']) && isset($_GPC['page']) ? intval($_GPC['page']) : 1;
            $max_page = ceil($total/$limit) < 1 ? $page : ceil($total/$limit);
            if ($page < 1) $page = 1;
            if ($page > $max_page) $page = $max_page;
            $offset = ($page - 1) * $limit;

            $question_list = pdo_fetchall("SELECT question_id,cat_id,question,answer FROM " . tablename('wherein_knowledge_question') . $where_sql . " ORDER BY `question_id` LIMIT " . $offset . "," . $limit, $where);
            if (!empty($question_list)) {
                foreach ($question_list as $key => $value) {
                    $question_list[$key]['answer'] = htmlspecialchars_decode($value['answer']);
                }
            }
            $out['question_list'] = $question_list;
            $out['total'] = intval($total);
            $this->outjson($out);
        }
        //一级分类名
        $cat_data = pdo_get('wherein_knowledge_category', array('cat_id' => $_GPC['cat_id']), array('parent_id', 'cat_name'));
        if ($cat_data['parent_id'] == 0) {
            $title = $cat_data['cat_name'];
        } else {
            $title = pdo_getcolumn('wherein_knowledge_category', array('cat_id' => $cat_data['parent_id']), 'cat_name');
        }

        include $this->template('question');
    }

    /*
     * 获取常见问题分类
     */
    public function getCommonQuestion() {
        global $_W;
        $where_sql = ' WHERE `uniacid`=:uniacid AND `is_common`=:is_common ';
        $where[':uniacid'] = $_W['uniacid'];
        $where[':is_common'] = 1;
        //初始化变量
        $category_list = $cate_list = $child_list = $parent_id_list = array();
        //查询所有的常见分类id
        $cat_ids_list = pdo_fetchall("SELECT cat_id FROM " . tablename('wherein_knowledge_question') . $where_sql, $where, 'cat_id');
        if (!empty($cat_ids_list)) {
            $cat_ids_list = return_array_value($cat_ids_list, 'cat_id');
            $cat_ids_str = implode(',', $cat_ids_list);
            //所有相关分类
            $cat_data = pdo_fetchall("SELECT cat_id,cat_name,parent_id FROM " . tablename('wherein_knowledge_category') . " WHERE `cat_id` IN (" . $cat_ids_str . ")");
            foreach ($cat_data as $key => $value) {
                if ($value['parent_id'] == 0) {
                    $cate_list[] = $value;
                } else {
                    $child_list[] = $value;//所有子类
                    $parent_id_list[] = $value['parent_id'];//子类id
                }
            }
            if (!empty($parent_id_list)) {
                $parent_cat_data = pdo_fetchall("SELECT cat_id,cat_name,parent_id FROM " . tablename('wherein_knowledge_category') . " WHERE `cat_id` IN (" . implode(',', $parent_id_list) . ")");
                foreach ($parent_cat_data as $v) {
                    array_push($cate_list, $v);//所有的父类
                }
            }
            foreach ($cate_list as $key => $value) {
                $category_list[$value['cat_id']] = $value;
                $child_list_arr = array();
                $category_list[$value['cat_id']]['child_list'] = $child_list_arr;
            }
            foreach ($child_list as $v) {
                $category_list[$v['parent_id']]['child_list'][] = $v;
            }
        }
        $out['category_list'] = $category_list;
        $out['total'] = count($category_list);
        $this->outjson($out);
    }
}